#include <iostream>
#include "Length.h"
#include "Unit.h"

using namespace LengthSpace;

Length::Length(int length, const UNIT& unit)
    :length_m(length),unit_m(unit>=sizeof(factor) ? INVAILD:unit)
{
}

int Length::convertToBaseUnit() const
{
   return length_m*factor[unit_m];
}

bool Length::operator==(const Length& length) const
{
    return convertToBaseUnit() == length.convertToBaseUnit();
}

Length operator+(const Length &a, const Length &b)
{  
return Length(a.convertToBaseUnit()+b.convertToBaseUnit(),FEET); 
}

int Length::calcLengthAtTopLevel() 
{
   remaider_m = convertToBaseUnit()%factor[MILE];
   return convertToBaseUnit()/factor[MILE];
}

int Length::calcLengthAtUnitLevel(int unit)
{
   int ret = remaider_m/factor[unit];
   remaider_m %= factor[unit];
   return ret; 
}

int Length::calcLength(int unit)
{
  if(unit == 0)
     return calcLengthAtTopLevel();
  else
     return calcLengthAtUnitLevel(unit);
}

void Length::displayAtUnitLevel(int unit) 
{
   if(calcLength(unit) != 0)
      std::cout<< calcLength(unit) << name[unit] << " ";
}

void Length::display() 
{
   for(int i = 0; i < sizeof(name)/sizeof(NAME); i++)
      displayAtUnitLevel(i);

   std::cout<<std::endl;
}

